package data.component;

import java.util.ArrayList;

public class Line {

    public Double[] start;
    public Double[] end;
    public Double length;
    public Double[] center;

    public Line(Double[] start, Double[] end, boolean isDirected) {
        if (isDirected) {
            this.start = start;
            this.end = end;
        } else {
            this.start = start[0] + start[1] < end[0] + end[1] ? start : end;
            this.end = start[0] + start[1] > end[0] + end[1] ? start : end;
        }

        this.center = new Double[]{(start[0] + end[0]) / 2, (start[1] + end[1]) / 2};
        this.length = GetLength(this.start, this.end);
    }

    public static Double GetLength(Double[] start, Double[] end) {
        return Math.sqrt(Math.pow(start[0] - end[0], 2) + Math.pow(start[1] - end[1], 2));
    }

    public static Double GetLength(Double[] start, ArrayList<Double> end) {
        return Math.sqrt(Math.pow(start[0] - end.get(0), 2) + Math.pow(start[1] - end.get(1), 2));
    }

    public String ToString() {
        //        return "start:[{(int)start[0]}, {(int)start[1]}]  end:[{(int)end[0]}, {(int)end[1]}] length:{Math.Round(length)}";
        return String.format("start:[%d, %d]  end:[%d,%d] length:{%d}", start[0].intValue(),start[1].intValue(), end[0].intValue(), end[1].intValue(), Math.round(length));
    }
}
